package com.wchat.serviceimpl;

import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import com.wchat.dto.OrderDto;
import com.wchat.enums.PayStatusEnum;
import com.wchat.enums.ResultEnum;
import com.wchat.exceptions.SellException;
import com.wchat.service.OrderService;
import com.wchat.service.PayService;
import com.wchat.utils.JsonUtils;
import com.wchat.utils.MoneyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Created by yangtianrui on 17/8/15.
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {

    private static final  String ORDER_NAME = "微信点餐订单";
    @Autowired
    private BestPayServiceImpl bestPayService;
    @Autowired
    private OrderService orderService ;

    /**
     *在线统一支付
     * @param orderDto
     */
    @Override
    public PayResponse create(OrderDto orderDto) {
        // 支付的请求参数封装在 payRequest 这些参数 可以从OrderDto里面获取到
     PayRequest payRequest = new PayRequest();
     payRequest.setOpenid(orderDto.getBuyerOpenid());//  获取openidbest
     payRequest.setOrderAmount(orderDto.getOrderAmount().doubleValue());
     payRequest.setOrderName(ORDER_NAME);
     payRequest.setOrderId(orderDto.getOrderId());
     payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
     log.info("[微信支付], 发起支付 request{}",JsonUtils.toJson(payRequest));
        //  调起支付请求 系统需要生成预支付订单 返回签名
     PayResponse response =  bestPayService.pay(payRequest);
     log.info("[微信支付结果], 发起支付 response{}",JsonUtils.toJson(response));
        return response;
    }

    /**
     * 微信异步通知
     * @param notifyData
     */
    @Override
    public PayResponse notify(String notifyData) {

        //  SDK中调用微信的异步通知 步骤:
        // 1.首先需要校验签名
        // 2.判读支付状态
        // 3.判断支付金额
        // 4.判断支付人   (支付人 == 下单人(买家) 看是否允许是同一个人。。。。)
       PayResponse payResponse =  bestPayService.asyncNotify(notifyData);
       log.info("[微信异步通知], 异步通知 payResponse={}",JsonUtils.toJson(payResponse));
        //   修改支付状态  修改之前需要先查询一下 订单是否存在
        OrderDto orderDto = orderService.findOne(payResponse.getOrderId());
        if(orderDto == null)
        {
            log.error("[异步通知查询订单不存在]orderDto={}",orderDto);
            throw new SellException(ResultEnum.NOTIFY_ORDER_ERROR);
        }
        //  判断金额是否一致
        if(MoneyUtils.Compare(orderDto.getOrderAmount().doubleValue(),payResponse.getOrderAmount()) == false)
        {
            log.error("[微信异步通知] 支付金额不一致,orderDtoAmount={},payAmount={}",
                    orderDto.getOrderAmount(),
                    payResponse.getOrderAmount());
            throw new SellException(ResultEnum.NOTIFY_TRANSACTION_AMOUNT_DISACCORD);
        }
        //orderService.cancel(orderDto);
        return  payResponse;

    }

    /**
     * 微信退款
     * @param orderDto
     * @return
     */
    @Override
    public RefundResponse refund(OrderDto orderDto) {
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderId(orderDto.getOrderId());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        refundRequest.setOrderAmount(orderDto.getOrderAmount().doubleValue());
        log.info("[微信退款] refundRequest={}",JsonUtils.toJson(refundRequest));
        RefundResponse refundResponse =bestPayService.refund(refundRequest);
        log.info("[微信退款] refundResponse={}",JsonUtils.toJson(refundResponse));
        return refundResponse;
    }


}
